//
//  UILabel+XHLKit.m
//  XHLKit
//
//  Created by qinyulun on 16/6/18.
//  Copyright © 2016年 leTian. All rights reserved.
//

#import "UILabel+XHLKit.h"

@implementation UILabel (XHLKit)

+ (UILabel *)labelWithRect:(CGRect)rect
                      text:(NSString *)text
                 textColor:(UIColor *)textColor
                  fontSize:(CGFloat)fontSize
             textAlignment:(NSTextAlignment)textAlignment {
    __autoreleasing UILabel *label = [[UILabel alloc] init];
    label.frame = rect;
    label.text = text;
    label.textColor = textColor;
    label.font = [UIFont systemFontOfSize:fontSize];
    label.textAlignment = textAlignment;
    label.numberOfLines = 0;
    return label;
}

+ (CGSize)sizeWithText:(NSString *)text
                  font:(UIFont *)font
               maxSize:(CGSize)maxSize {
    return [text length] > 0
    ? [text boundingRectWithSize:maxSize
                         options:NSStringDrawingUsesLineFragmentOrigin
                      attributes:@{
                                   NSFontAttributeName : font
                                   } context:nil].size
    : CGSizeZero;
}

- (CGSize)sizeWithFont:(UIFont *)font maxSize:(CGSize)maxSize {
    return [UILabel sizeWithText:self.text font:font maxSize:maxSize];
}


- (CGSize)boundingRectWithSize:(CGSize)size
{
    NSDictionary *attribute = @{NSFontAttributeName: self.font};
    
    CGSize retSize = [self.text boundingRectWithSize:size
                                             options:
                      NSStringDrawingTruncatesLastVisibleLine |
                      NSStringDrawingUsesLineFragmentOrigin |
                      NSStringDrawingUsesFontLeading
                                          attributes:attribute
                                             context:nil].size;
    
    return retSize;
}

+ (CGFloat)getStingWidth:(NSString *)string font:(UIFont*)font labelHeight:(CGFloat)labelHeight
{
    UILabel *lable=[UILabel new];
    lable.text=string;
    lable.font=font;
    CGSize size=[lable boundingRectWithSize:CGSizeMake(0, labelHeight)];
    return size.width;
    
}


@end
